//
//  GlobalTabel.m
//  GYVVUser
//
//  Created by LGSC on 2018/4/25.
//  Copyright © 2018年 gongyou04. All rights reserved.
//

#import "GlobalTabel.h"


@implementation GlobalTabel

- (void)willMoveToSuperview:(UIView *)newSuperview {
    [super willMoveToSuperview:newSuperview];
    //关闭iOS11默认开启的self sizing
    [self closeSelfSizing];
}

/**关闭预估行高功能*/
- (void)closeSelfSizing {
    self.estimatedRowHeight = 0;
    self.estimatedSectionHeaderHeight = 0;
    self.estimatedSectionFooterHeight = 0;
    
    //解决iOS11下白色状态栏问题
    if (@available(iOS 11.0, *)) {
        self.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }
    
    if (ssiPhoneX) {
        UIView *footer = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_W, STATUSBAR_BOTTOM_H)];
        self.tableFooterView = footer;
    }
}

/**是否显示header*/
-(void)setShow_header:(BOOL)show_header
{
    if (show_header) {
        _show_header = show_header;
        my_header = [[GlobleHeaderView alloc]initWithFrame:CGRectMake(0, -1000, SCREEN_W, 1000)];
        [self addSubview:my_header];
        [self addObserver:self forKeyPath:@"contentOffset" options:NSKeyValueObservingOptionNew context:nil];
    }
}

/**KVO监听table的滚动事件*/
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    if ([keyPath isEqualToString:@"contentOffset"]) {
        if (self.contentOffset.y < 0) {
            my_header.header_lb.alpha = fabsf((float)self.contentOffset.y)/(float)headerLbAlphaConstant;
        }else {
            my_header.header_lb.alpha = .0;
        }
        if (self.contentOffset.y < -10) {
            [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault animated:YES];
        }else {
            [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent animated:YES];
        }
    }
}

-(void)dealloc{
    
    if (_show_header) {
        [self removeObserver:self forKeyPath:@"contentOffset"];
    }
    if (_errorView) {
        _errorView = nil;
    }
    
    if (_NoResult) {
        _NoResult = nil;
    }
    NSLog(@"***********GlobalTabel基类的%@ dealloc***********",[NSString stringWithUTF8String:class_getName([self class])]);
}

-(BOOL)isLoadingNow{
    
    return is_loading;
}
//加载动画
-(void)ShowLoading{
    
    is_loading = YES;
    
    self.HUD = [MBProgressHUD showHUDAddedTo:self animated:YES];
    self.HUD.labelText = @"正在加载...";
}

-(void)HideLoading{
    
    is_loading = NO;
    
    if (self.HUD!=nil) {
        self.HUD.delegate = nil;
        [self.HUD removeFromSuperview];
        self.HUD = nil;
    }
    
}

-(void)ShowNewMsg:(NSString*)str
{
    cnhbMsgAlert *alert = [[cnhbMsgAlert alloc] init];
    [alert setTextColor:[UIColor whiteColor]];
    [alert showMessage:str];
}

//-(void)LGShowMsg:(NSString*)str time:(int)time{
//    [SVProgressHUD showSuccessWithStatus:str duration:time];
//}

-(void)ShowV3Msg:(NSString *)str
{
    LG_alertview=[[UIAlertView alloc] initWithTitle:@"提示信息" message:str delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
    LG_alertview.tag = 666666;
    [LG_alertview show];
}

-(void)ShowNoDataView:(UIView *)view Orgin:(CGFloat)Y
{
    if (!_NoResult) {
        NSArray *array=[[NSBundle mainBundle] loadNibNamed:@"NoDataView" owner:self options:nil];
        self.NoResult=[array objectAtIndex:0];
        self.NoResult.frame = CGRectMake(0, Y, view.frame.size.width, view.frame.size.height-Y);
        if ([_NodataStr CheckNSStringData]) {
            self.NoResult.tipLabel.text = _NodataStr;
        }
        [view addSubview:_NoResult];
    }
}
-(void)HideNoDataView
{
    if (_NoResult) {
        [_NoResult removeFromSuperview];
        self.NoResult=nil;
    }
}
-(void)ShowerrorView:(UIView *)view Orgin:(CGFloat)Y
{
    if (_errorView == nil) {
        _errorView  = [[[NSBundle mainBundle] loadNibNamed:xibNameTryAgain owner:self options:nil]lastObject];
        _errorView.frame = CGRectMake(0, Y, view.frame.size.width, view.frame.size.height-Y);
        _errorView.backgroundColor = [UIColor whiteColor];
        [_errorView loadingError];
        _errorView.delegate = self;
    }
    [view addSubview:_errorView];
}
-(void)HideerrorView
{
    if (_errorView) {
        [_errorView removeFromSuperview];
        self.errorView=nil;
    }
}

-(void)ScreenBtnClicked
{
    [self HideNoDataView];
    [self HideerrorView];
}

-(void)ShowNoDataBtnView:(UIView *)view Orgin:(CGFloat)Y Title:(NSString *)Title BtnTitle:(NSString *)BtnTitle
{
    if (_NoDataBtnView == nil) {
        _NoDataBtnView  = [[[NSBundle mainBundle] loadNibNamed:@"NoDataBtnView" owner:self options:nil]lastObject];
        _NoDataBtnView.frame = CGRectMake(0, Y, view.frame.size.width, view.frame.size.height-Y);
        _NoDataBtnView.backgroundColor = [UIColor whiteColor];
        if ([Title CheckNSStringData]) {
            _NoDataBtnView.tipLabel.text = Title;
        }
        if ([BtnTitle CheckNSStringData]) {
            _NoDataBtnView.workLabel.text = BtnTitle;
        }
        _NoDataBtnView.delegate = self;
    }
    [view addSubview:_NoDataBtnView];
}

/*
 * 约到工作占位图专用
 */
-(void)ShowNoDataBtnView1:(UIView *)view Orgin:(CGFloat)Y Title:(NSString *)Title BtnTitle:(NSString *)BtnTitle{
    if (_NoDataBtnView == nil) {
        _NoDataBtnView  = [[[NSBundle mainBundle] loadNibNamed:@"NoDataBtnView" owner:self options:nil]lastObject];
        _NoDataBtnView.frame = CGRectMake(0, Y, view.frame.size.width, view.frame.size.height-Y);
        _NoDataBtnView.backgroundColor = [UIColor whiteColor];
        _NoDataBtnView.back_img.image = [UIImage imageNamed:@"yueJob"];
        _NoDataBtnView.workLabel.backgroundColor = [UIColor whiteColor];
        _NoDataBtnView.distance_height.constant = -10;
        _NoDataBtnView.o_label.text = @"哦";
        _NoDataBtnView.line_view.backgroundColor = [UIColor colorWithHexString:@"4A90E2"];
        _NoDataBtnView.workLabel.textColor = [UIColor colorWithHexString:@"4A90E2"];
        if ([Title CheckNSStringData]) {
            _NoDataBtnView.tipLabel.text = Title;
        }
        if ([BtnTitle CheckNSStringData]) {
            _NoDataBtnView.workLabel.text = BtnTitle;
        }
        _NoDataBtnView.delegate = self;
    }
    [view addSubview:_NoDataBtnView];
}


-(void)HideNoDataBtnView
{
    if (_NoDataBtnView) {
        [_NoDataBtnView removeFromSuperview];
        _NoDataBtnView=nil;
    }
}

-(void)ShowV3Msg:(NSString *)str title:(NSString *)title buttonTitle:(NSString *)btntitle;
{
    if (!title) {
        title = @"信息提示";
    }
    if (!btntitle) {
        btntitle = @"确定";
    }
    
    UIAlertView *alert=[[UIAlertView alloc] initWithTitle:title message:str delegate:nil cancelButtonTitle:btntitle otherButtonTitles:nil, nil];
    alert.tag = 666666;
    [alert show];
}

@end
